package org.hamster.multidropbox.dropbox.model
{
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLLoader;
	
	import org.hamster.dropbox.DropboxClient;
	import org.hamster.dropbox.DropboxEvent;
	import org.hamster.dropbox.models.AccountInfo;
	import org.hamster.dropbox.models.DropboxFile;
	import org.hamster.multidropbox.dropbox.constants.AppDropboxClientStatus;
	import org.hamster.multidropbox.dropbox.event.AppDropboxEvent;
	import org.hamster.multidropbox.dropbox.service.DropboxTreeCacheService;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxClient;
	import org.hamster.multidropbox.dropbox.vo.AppDropboxFile;
	import org.puremvc.as3.patterns.proxy.Proxy;
	
	public class DropboxClientProxy extends Proxy
	{
		public static const NAME:String = "org.hamster.multidropbox.dropbox.model.DropboxClientProxy";
		
		public function DropboxClientProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function requestToken(dropboxClient:DropboxClient):void
		{
			dropboxClient.requestToken();
			dropboxClient.addEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, requestTokenResultHandler);
			dropboxClient.addEventListener(DropboxEvent.REQUEST_TOKEN_FAULT, requestTokenFaultHandler);
		}
		
		private function requestTokenResultHandler(event:DropboxEvent):void
		{
			var dropboxClient:DropboxClient = event.currentTarget as DropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, requestTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_FAULT, requestTokenFaultHandler);
			sendNotification(AppDropboxEvent.DROPBOX_REQUEST_TOKEN_RESULT, dropboxClient.config);
		}
		
		private function requestTokenFaultHandler(event:DropboxEvent):void
		{
			var dropboxClient:DropboxClient = event.currentTarget as DropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_RESULT, requestTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.REQUEST_TOKEN_FAULT, requestTokenFaultHandler);
			sendNotification(AppDropboxEvent.DROPBOX_REQUEST_TOKEN_FAULT, event.resultObject);
		}
		
		public function accessToken(dropboxClient:AppDropboxClient):void
		{
			dropboxClient.accessToken();
			dropboxClient.addEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, accessTokenResultHandler);
			dropboxClient.addEventListener(DropboxEvent.ACCESS_TOKEN_FAULT, accessTokenFaultHandler);
		}
		
		private function accessTokenResultHandler(event:DropboxEvent):void
		{
			var dropboxClient:DropboxClient = event.currentTarget as DropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, accessTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_FAULT, accessTokenFaultHandler);
			sendNotification(AppDropboxEvent.DROPBOX_ACCESS_TOKEN_RESULT, dropboxClient.config);
		}
		
		private function accessTokenFaultHandler(event:DropboxEvent):void
		{
			var dropboxClient:DropboxClient = event.currentTarget as DropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_RESULT, accessTokenResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCESS_TOKEN_FAULT, accessTokenFaultHandler);
			sendNotification(AppDropboxEvent.DROPBOX_ACCESS_TOKEN_FAULT, event.resultObject);
		}
		
		public function accountInfo(dropboxClient:AppDropboxClient):void
		{
			dropboxClient.accountInfo();
			dropboxClient.addEventListener(DropboxEvent.ACCOUNT_INFO_RESULT, accountInfoResultHandler);
			dropboxClient.addEventListener(DropboxEvent.ACCOUNT_INFO_FAULT,  accountInfoFaultHandler);
		}
		
		private function accountInfoResultHandler(event:DropboxEvent):void
		{
			var dropboxClient:AppDropboxClient = event.currentTarget as AppDropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.ACCOUNT_INFO_RESULT, accountInfoResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCOUNT_INFO_FAULT,  accountInfoFaultHandler);
			dropboxClient.accountDetail = event.resultObject as AccountInfo;
			dropboxClient.status = AppDropboxClientStatus.AVAILABLE;
			sendNotification(AppDropboxEvent.DROPBOX_ACCOUNT_INFO_RESULT, dropboxClient);
		}
		
		private function accountInfoFaultHandler(event:DropboxEvent):void
		{
			var dropboxClient:AppDropboxClient = event.currentTarget as AppDropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.ACCOUNT_INFO_RESULT, accountInfoResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.ACCOUNT_INFO_FAULT,  accountInfoFaultHandler);
			dropboxClient.status = AppDropboxClientStatus.NOT_AVAILABLE;
			sendNotification(AppDropboxEvent.DROPBOX_ACCOUNT_INFO_FAULT, dropboxClient);
		}
		
		public function metadata(dropboxClient:AppDropboxClient, path:String):void
		{
			var result:AppDropboxFile = DropboxTreeCacheService.instance.getDropboxItem(dropboxClient.uid, path);
			
			if (result == null) {
				if (path.charAt(0) == "/") {
					path = path.substr(1);
				}
				dropboxClient.metadata(path, 2000, "", true, false);
				dropboxClient.addEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
				dropboxClient.addEventListener(DropboxEvent.METADATA_FAULT, metadataFaultHandler);
			} else {
				sendNotification(AppDropboxEvent.DROPBOX_METADATA_RESULT, result);
			}
		}
		
		private function metadataResultHandler(event:DropboxEvent):void
		{
			var dropboxClient:AppDropboxClient = event.currentTarget as AppDropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.METADATA_FAULT,  metadataFaultHandler);
			
			var dropboxFile:DropboxFile = DropboxFile(event.resultObject);
			var appDropboxFile:AppDropboxFile = new AppDropboxFile(dropboxClient, dropboxFile);
			DropboxTreeCacheService.instance.putDropboxItem(dropboxClient.uid, appDropboxFile);
			
			if (dropboxFile.contents) {
				var childFileList:Array = new Array();
				for each (var child:DropboxFile in dropboxFile.contents) {
					var childAppDropboxFile:AppDropboxFile = new AppDropboxFile(dropboxClient, child);
					childFileList.push(childAppDropboxFile);
				}
				appDropboxFile.contents = childFileList;
			}
			
			sendNotification(AppDropboxEvent.DROPBOX_METADATA_RESULT, appDropboxFile);
		}
		
		private function metadataFaultHandler(event:DropboxEvent):void
		{
			var dropboxClient:DropboxClient = event.currentTarget as DropboxClient;
			dropboxClient.removeEventListener(DropboxEvent.METADATA_RESULT, metadataResultHandler);
			dropboxClient.removeEventListener(DropboxEvent.METADATA_FAULT,  metadataFaultHandler);
			sendNotification(AppDropboxEvent.DROPBOX_METADATA_FAULT, event.resultObject);
		}
	}
}